package com.dxy.chatserver.config;

import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import lombok.Data;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * mongo client 构建工具
 *
 * @author: none
 * @date: 2022/1/8
 **/
@Data
public class MongoBuilder {

    /**
     * mongo client 构建
     *
     * @param uri uri
     * @return
     */
    public static MongoClient client(String uri) {
        MongoClientURI mongoClientURI = new MongoClientURI(uri, MongoClientOptions.builder()
                .heartbeatFrequency(1000)
                .maxConnectionIdleTime(86400000)
                .heartbeatSocketTimeout(1000)
                .connectTimeout(60000)
                .maxWaitTime(60000)
        );
        return MongoClients.create(mongoClientURI.getURI());
    }

    /**
     * 构建mongo template
     *
     * @param client mongo client
     * @param dbName 库名
     * @return
     */
    public static MongoTemplate template(MongoClient client, String dbName) {
        return new MongoTemplate(client, dbName);
    }

}
